package code._20stream._04endOperations;

import code._20stream.bean.Student;
import code._20stream.bean.StudentList;
import org.junit.Test;

import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;

public class EndOperationTest {
    @Test
    public void testAnyMatch(){
        List<Student> studentList = new StudentList().studentList;
        boolean flag = studentList.stream()
                .filter(student -> student.getAddress() == Student.Address.BJ)
                .anyMatch(student -> student.getHeight() > 187);
        System.out.println(flag);

    }

    @Test
    public void testAllMatch(){
        List<Student> studentList = new StudentList().studentList;
        boolean flag = studentList.stream()
                .filter(student -> student.getAddress() == Student.Address.BJ)
                .allMatch(new Predicate<Student>() {
                    @Override
                    public boolean test(Student student) {
                        return student.getHeight() > 200;
                    }
                });
        System.out.println(flag);
    }

    @Test
    public void testNoneMatch(){
        List<Student> studentList = new StudentList().studentList;
        boolean flag = studentList.stream()
                .filter(new Predicate<Student>() {
                    @Override
                    public boolean test(Student student) {
                        return student.getAddress() == Student.Address.BJ;
                    }
                })
                .noneMatch(new Predicate<Student>() {
                    @Override
                    public boolean test(Student student) {
                        return student.getHeight() > 200;
                    }
                });
        System.out.println("flag = " + flag);
    }


    @Test
    public void testFindAny(){
        List<Student> studentList = new StudentList().studentList;
        Optional<Student> optional = studentList.stream()
                .filter(student -> student.getAddress() == Student.Address.BJ)
                .findAny();
        System.out.println(optional );
        System.out.println(optional.isPresent());
    }
}
